package com.qf.kdgroup.gdbackend.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.kdgroup.gdbackend.dto.ResultBean;
import com.qf.kdgroup.gdbackend.entity.Auth;
import com.qf.kdgroup.gdbackend.entity.RoleAuth;
import com.qf.kdgroup.gdbackend.mapper.AuthMapper;
import com.qf.kdgroup.gdbackend.mapper.RoleAuthMapper;
import com.qf.kdgroup.gdbackend.service.AuthService;
import com.qf.kdgroup.gdbackend.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthMapper authMapper;

    @Autowired
    private RoleAuthMapper roleAuthMapper;

    @Override
    public PageInfo selectAllByPage(PageVO pageVO) {
        PageHelper.startPage(pageVO.getPageNum().intValue(),pageVO.getPageSize().intValue());
        List<Auth> auths = authMapper.selectList(pageVO.getKeyword());

        PageInfo<Auth> pageInfo = new PageInfo<>(auths);
        return pageInfo;
    }

    @Override
    public ResultBean deleteByAuthId(Long authid) {

        List<RoleAuth> roleAuths = roleAuthMapper.selectListByAuthId(authid);

        if (roleAuths.size()>0) {
            return ResultBean.error("删除失败,有角色关联该权限");
        }

        int res = authMapper.deleteByPrimaryKey(authid);

        return res > 0 ? ResultBean.success("删除成功") : ResultBean.error("删除失败");
    }

    @Override
    public ResultBean add(Auth auth) {

        int res = authMapper.insertSelective(auth);

        return res > 0 ? ResultBean.success() : ResultBean.error();
    }

    @Override
    public Auth findByAuthId(Long authid) {

        Auth auth = authMapper.selectByPrimaryKey(authid);

        return auth;
    }

    @Override
    public ResultBean update(Auth auth) {

        int res = authMapper.updateByPrimaryKeySelective(auth);

        return res > 0 ? ResultBean.success() : ResultBean.error();
    }
}
